package jp.bsws.action.master;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.JugyoinBean;
import jp.bsws.bean.KengenBean;
import jp.bsws.bean.KeyValueBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.BSWSDao;
import jp.bsws.dao.JugyoinDao;
import jp.bsws.dao.JugyoinRelationDao;
import jp.bsws.dao.KengenDao;
import jp.bsws.model.JugyoinModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class JugyoinMasterAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private List<KeyValueBean> sansyoTenpoList;
//	private String strSansyoTenpoCod;

	private List<JugyoinBean> jugyoinBeanList;
	private Integer intTaisyoSeq = null;

	public void validateUpdateJugyoin() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//必須チェック
		for(int i =0; i < jugyoinBeanList.size(); i++){
			JugyoinBean jugyoinBean = jugyoinBeanList.get(i);
//			if( intTaisyoSeq == jugyoinBean.getSeq() && jugyoinBean.isChkEdit()){
//				if (BSWSUtils.isBlankNull(jugyoinBean.getStrJugyoinKnj())) {
//					addFieldError("jugyoinBeanList[" + i + "].strJugyoinKnj", (i + 1) + "行目の従業員名称を入力して下さい。");
//				}
//				break;
//			}
		}
	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);
//		bSWSSession.setStrSansyoTenpoCod(getStrSansyoTenpoCod());
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	/**
	 * 従業員追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String add() throws Exception{

		init();

		KengenDao kengenDao = KengenDao.getInstance();

		Connection con = DBUtils.getConnection();
		try{
			for(JugyoinBean jugyoinBean : this.jugyoinBeanList) {
				jugyoinBean.setStrGroupCod(bSWSSession.getStrGroupCod());
				jugyoinBean.setStrTenpoCod(bSWSSession.getStrSansyoTenpoCod());
				jugyoinBean.setTenpoList(bSWSSession.getSansyoTenpoList());
				jugyoinBean.setKengenList(kengenDao.getKengenList(con, bSWSSession.getStrGroupCod(), null));
			}
			//従業員行追加
			JugyoinBean jugyoinBean = new JugyoinBean();
			jugyoinBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			jugyoinBean.setStrTenpoCod(bSWSSession.getStrSansyoTenpoCod());
			jugyoinBean.setTenpoList(bSWSSession.getSansyoTenpoList());
			jugyoinBean.setKengenList(kengenDao.getKengenList(con, bSWSSession.getStrGroupCod(), null));
			this.jugyoinBeanList.add(jugyoinBean);
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String updateJugyoin() throws Exception{

		JugyoinDao jugyoinDao = JugyoinDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{

			//従業員情報更新対象取得
			for(JugyoinBean jugyoinBean : jugyoinBeanList) {
				int cnt = 0;
				if(jugyoinBean.getSeq() != null){
					//店舗情報更新
					cnt = jugyoinDao.updateJugyoin(con, bSWSSession, jugyoinBean);
				}else{
					if(BSWSUtils.isNotBlankNull(jugyoinBean.getStrTenpoCod())
							&& BSWSUtils.isNotBlankNull(jugyoinBean.getStrJugyoinKnj())
							&& BSWSUtils.isNotBlankNull(jugyoinBean.getStrPassword())
							&& jugyoinBean.getIntKengenSeq() != null
							&& BSWSUtils.isNotBlankNull(jugyoinBean.getStrNyutenYmd())
							&& BSWSUtils.isNotBlankNull(jugyoinBean.getStrTaitenYmd())
							&& BSWSUtils.isNotBlankNull(jugyoinBean.getStrHyojiJun())
					){
						//店舗情報更新
						cnt = jugyoinDao.insertJugyoin(con, null, bSWSSession, jugyoinBean);
					}
				}
				if(cnt == 0){
					//処理なし
				}
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String deleteJugyoin() throws Exception{

		JugyoinDao jugyoinDao = JugyoinDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//従業員情報削除
			int cnt = jugyoinDao.deleteJugyoin(con, intTaisyoSeq);
			if(cnt == 0){
				//処理なし
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{

		//従業員マスタ情報を取得
		getJugyoinJoho(con);
//		KengenDao kengenDao = KengenDao.getInstance();

//		//従業員行追加
//		JugyoinBean jugyoinBean = new JugyoinBean();
//		jugyoinBean.setStrGroupCod(bSWSSession.getStrGroupCod());
//		jugyoinBean.setStrTenpoCod(bSWSSession.getStrSansyoTenpoCod());
//		jugyoinBean.setTenpoList(bSWSSession.getSansyoTenpoList());
//		jugyoinBean.setKengenList(kengenDao.getKengenList(con, bSWSSession.getStrGroupCod(), null));
//		jugyoinBeanList.add(jugyoinBean);
	}

	private void getJugyoinJoho(Connection con) throws Exception{

		BSWSDao bSWSDao = BSWSDao.getInstance();
		KengenDao kengenDao = KengenDao.getInstance();
		this.jugyoinBeanList = new ArrayList<JugyoinBean>();
		this.jugyoinBeanList.clear();
		//従業員マスタ情報を取得
		List<JugyoinModel> jugyoinList = bSWSDao.getJugyoinJoho(con, null, bSWSSession.getStrGroupCod(), null, null, null);
		for(int i = 0; i < jugyoinList.size(); i++){
			JugyoinBean jugyoinBean = new JugyoinBean();
			jugyoinBean.setSeq(jugyoinList.get(i).getSeq());
			jugyoinBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			jugyoinBean.setStrTenpoCod(jugyoinList.get(i).getStrTenpoCod());
			jugyoinBean.setStrJugyoinCod(jugyoinList.get(i).getStrJugyoinCod());
			jugyoinBean.setStrJugyoinKnj(jugyoinList.get(i).getStrJugyoinKnj());
			jugyoinBean.setStrPassword(jugyoinList.get(i).getStrPassWord());
			jugyoinBean.setIntKengenSeq(jugyoinList.get(i).getIntKengenSeq());
			jugyoinBean.setStrNyutenYmd(String.valueOf(jugyoinList.get(i).getIntNyutenYmd()));
			jugyoinBean.setStrTaitenYmd(String.valueOf(jugyoinList.get(i).getIntTaitenYmd()));
			jugyoinBean.setStrHyojiJun(String.valueOf(jugyoinList.get(i).getIntHyojiJun()));
			jugyoinBean.setTenpoList(bSWSSession.getSansyoTenpoList());
			jugyoinBean.setKengenList(kengenDao.getKengenList(con, bSWSSession.getStrGroupCod(), null));
			this.jugyoinBeanList.add(jugyoinBean);
		}
	}

	public BSWSSession getBSWSSession() {
		return bSWSSession;
	}

	public void setBSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public List<JugyoinBean> getJugyoinBeanList() {
		return jugyoinBeanList;
	}

	public void setJugyoinBeanList(List<JugyoinBean> jugyoinBeanList) {
		this.jugyoinBeanList = jugyoinBeanList;
	}

	public List<KeyValueBean> getSansyoTenpoList() {
		return sansyoTenpoList;
	}

	public void setSansyoTenpoList(List<KeyValueBean> sansyoTenpoList) {
		this.sansyoTenpoList = sansyoTenpoList;
	}

	public Integer getIntTaisyoSeq() {
		return intTaisyoSeq;
	}

	public void setIntTaisyoSeq(Integer intTaisyoSeq) {
		this.intTaisyoSeq = intTaisyoSeq;
	}
}